package begin.view 
{
	import begin.BeginC;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import interfaces.IOperate;
	import morn.core.components.BitmapText;
	import morn.core.components.Box;
	import morn.core.handlers.Handler;
	import org.airmvc.Dispatcher;
	import ui.begin.EnterGameUI;
	
	/**
	 * 进入游戏
	 * @author WLDragon 2014-03-03
	 */
	public class EnterGame extends EnterGameUI implements IOperate
	{
		
		public function EnterGame() 
		{
			super();
			//this.mouseEnabled = false;
			//addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			//addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(e:MouseEvent):void 
		{
			var t:BitmapText = e.target as BitmapText;
			if (t)
			{
				Dispatcher.send(BeginC.ORDER,this,curItem.name);//选择参数，发送指令
			}
		}
		
		private function onMouseOver(e:MouseEvent):void 
		{
			var t:BitmapText = e.target as BitmapText;
			if (t)
			{
				switch (t.name) 
				{
					case "easy":
						index = 0;
					break;
					case "normal":
						index = 1;
					break;
					case "difficult":
						index = 2;
					break;
					case "start":
						index = 0;
					break;
					case "read":
						index = 1;
					break;
				}
			}
		}
		
		public function execute (keyCode:int) : void
		{
			switch (keyCode) 
			{
				case Keyboard.SPACE:
					if (clpDawn.visible)
						stop();
					else
						Dispatcher.send(BeginC.ORDER,this,curItem.name);//选择参数，发送指令
				break;
				case Keyboard.W:
					index--;
				break;
				case Keyboard.S:
					index++;
				break;
				default:
			}
		}
		
		private function stop():void
		{
			clpDawn.visible = false;
			clpArrow.visible = true;
			clpDawn.dispose();
			curBox = boxFirstMenu;
			boxFirstMenu.visible = true;
			index = 0;
		}
		
		public function openSecondMenu():void
		{
			boxFirstMenu.visible = false;
			boxSecondMenu.visible = true;
			curBox = boxSecondMenu;
			_index = -1;
			index = 0;
		}
		
		public function play():void
		{
			clpLogo.visible = true;
			clpLogo.playFromTo(-1,-1,new Handler(onPlayComplete));
			
			//flag = 3;//跳过动画，发布时打开
			//clpDawn.visible = true;
			//onPlayComplete();//
		}
		
		private function onPlayComplete():void 
		{
			switch (flag) 
			{
				case 1:
					clpLogo.visible = false;
					clpLogo.dispose();
					clpWord.visible = true;
					flag = 2;
					clpWord.playFromTo(-1,-1,new Handler(onPlayComplete));
				break;
				case 2:
					clpDawn.visible = true;
					boxWords.visible = true;
					flag = 3;
					clpDawn.playFromTo(-1,-1,new Handler(onPlayComplete));
				break;
				case 3:
					Kyb.setOperater(this);
				break;
				default:
			}
		}
		
		private function set index(value:int):void 
		{
			if (value < 0 || value >= curBox.numChildren || value == _index)
				return;
			
			curItem = curBox.getChildAt(value) as BitmapText;
			clpArrow.x = curBox.x - 16;
			clpArrow.y = curBox.y + curItem.y + 2;
			_index = value;
		}
		
		private function get index():int 
		{
			return _index;
		}
		
		
		/**播放动画标志1-LOGO,2-三国志文字,3-黎明*/
		private var flag:int = 1;
		private var curItem:BitmapText;
		private var curBox:Box;
		private var _index:int = -1;
		
	}

}